<?php 
$reqPath='';
//require_once("include/authen.php");
require_once($reqPath."class/m_report.php");
require_once($reqPath.'fpdf/fpdf.php');

class PDF extends FPDF
{
	var $datefrom;
	var $dateto;
	
	function setHeaderCriteria($dtfrom , $dtto){
		$this->datefrom=$dtfrom;
		$this->dateto=$dtto;
	}
	function Header()
	{
		//Arial bold 15
		$this->SetFont('tahoma','B',12);
		//Title
		$this->Cell(0,10,iconv( 'UTF-8','cp874' , 'รายงานยอดขายประจำปี'),0,0,'C');
		$this->Ln(5);
		$this->SetFont('tahoma','B',11);
		$this->Cell(0,10,iconv( 'UTF-8','cp874' , 'ข้อมูลตั้งแต่  '. $this->datefrom .' ถึง '. $this->dateto),0,0,'C');
		//Line break
		$this->Ln(15);
	}

	//Page footer
	function Footer()
	{
		//Position at 1.5 cm from bottom
		$this->SetY(-15);
		//Arial italic 8
		$this->SetFont('Arial','I',8);
		//Page number
		$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'R');
	}

	//Colored table
	function GenTable($header,$data=null)
	{
		$w=array(12,15,15,16,15,15,15,15,15,16,16,16,16,14,15,16,15,15,15);
		$bln="";
		//header
		
		$icount=count($data);
		$k=1;
		foreach ($data as $val){
			
			$this->SetTextColor(0);
			$this->SetFont('','B');
			$this->SetFontSize('10');
			if ( $k==1)
				$border='';
			else 
				$border='LR';
				
			$k++;
			$this->Cell(array_sum($w),10, iconv( 'UTF-8','cp874' , "สาขา -  " . $val["BranchName"] ),$border,0,'L',false);
			$this->Ln();
			 
			//Colors, line width and bold font
			$this->SetFillColor(20,114,218);
			$this->SetTextColor(255);
			$this->SetDrawColor(114,175,242);
			$this->SetLineWidth(.1);
			$this->SetFont('tahoma','');
			$this->SetFontSize('8');
			$sumwidth =0;
			for($i=0;$i<count($header);$i++){
				
				if ($i<14){
					$sumwidth += $w[$i];
					$h=14;
					 $this->Cell($w[$i],$h,iconv( 'UTF-8','cp874' , $header[$i]),1,0,'C',true);
				}else{
					$h=7;
					if ($i==14){
						$this->Cell($w[14] + $w[15] + $w[16],$h,iconv( 'UTF-8','cp874' , 'รายได้จากการขาย'),1,0,'C',true);
						
					}else if ($i==17){
						$this->Cell($w[17] ,7,iconv( 'UTF-8','cp874' , 'รับชำระหนี้'),1,0,'C',true);
						$this->Cell($w[18] ,7,iconv( 'UTF-8','cp874' , 'ลูกหนี้'),'LR',0,'C',true);
						$this->Ln();
						$this->SetX( $sumwidth + 5  ); //5 margin
						
						for($k=14;$k<17;$k++){
							$this->Cell($w[$k],$h,iconv( 'UTF-8','cp874' , $header[$k]),1,0,'C',true);
						}
						$this->Cell($w[17],$h,'','LR',0,'C',true);
						$this->Cell($w[18],$h,'','LR',0,'C',true);
					} 
				}
			}	
			$this->Ln();
			 
		
			//Color and font restoration
			$this->SetFillColor(224,235,255);
			$this->SetTextColor(0);
			$this->SetFont('');
			$this->SetFontSize('7');
			//detail
			$fill=false;
			$i=1;
			$productTotal=0;$productDiscountTotal=0; 
			$discountTotal=0;
			$toalNetTotal=0;
			$treatmentTotal =0;$treatmentDiscountTotal =0;
			$payTotalTotal=0;
			$payTotalTotal=0;
			$setTotal =0;$setDiscountTotal =0;$payTotalTotal=0;$paySmartTotal=0;$creditCashTotal=0;
			$courseTotal =0;$courseDiscountTotal =0;$creditTotal=0;$creditSmartTotal=0;
			$cutCourseTotal =0;
			$payTotal =0;
			$smartpurse=0;
			foreach ($val['summary'] as $d){
				$payTotal = $d['Product'] + $d['Treatment'] + $d['Set'] + $d['CourseCut'];
				 
				$this->Cell($w[0],6,iconv( 'UTF-8','cp874' , $d["Date"]),'LR',0,'L',$fill);
				$this->Cell($w[1],6, number_format( $d['Product'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[2],6, number_format( $d['ProductDiscount'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[3],6, number_format( $d['Treatment'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[4],6, number_format( $d['TreatmentDiscount'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[5],6, number_format( $d['Set'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[6],6, number_format( $d['SetDiscount'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[7],6, number_format( $d['Course'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[8],6, number_format( $d['CourseDiscount'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[9],6, number_format( $d['CourseCut'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[10],6, number_format( $d['Discount'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[11],6, number_format( $d['TotalNet'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[12],6, number_format( $payTotal ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[13],6, number_format( $d['smartpurse'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[14],6, number_format( $d['PayCash'] +  $d['smartpurse'],2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[15],6, number_format( $d['PayCredit'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[16],6, number_format( $d['PaySmartPurse'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[17],6, number_format( $d['Credit'] ,2,'.',','),'LR',0,'R',$fill);
				$this->Cell($w[18],6, number_format( $d['Remain'] ,2,'.',','),'LR',0,'R',$fill);
				
				$this->Ln();
				$fill=!$fill;
				$i++;		
				
				$productTotal += $d['Product'];
				$productDiscountTotal +=$d['ProductDiscount'];
				$treatmentTotal +=  $d['Treatment'];
				$treatmentDiscountTotal +=  $d['TreatmentDiscount'];
				$setTotal +=  $d['Set'];
				$setDiscountTotal +=  $d['SetDiscount'];
				$courseTotal +=  $d['Course'];
				$courseDiscountTotal +=  $d['CourseDiscount'];
				$cutCourseTotal +=  $d['CourseCut'];
				$discountTotal +=  $d['Discount'];
				$toalNetTotal +=  $d['TotalNet'];
				$payTotalTotal +=  $payTotal;
				$payCashTotal +=  $d['PayCash'] + $d['smartpurse'];
				$payCreditTotal += $d['PayCredit'];
				$paySmartTotal += $d['PaySmartPurse'];
				$creditCashTotal += $d['CreditCash'];
				$creditTotal += $d['Credit'];
				$creditSmartTotal += $d['Remain'];
				$smartpurse+=$d['smartpurse'];
			}
			$this->SetFillColor(255,204,102);
						$this->SetFont('Tahoma');
			$this->SetFontSize('7');
			$this->Cell(array_sum($w),0,'','T' ,true );
			$this->Ln();
			//Total summary 
			$this->Cell($w[0]  ,6, iconv( 'UTF-8','cp874' ,'รวม'),'LR',0,'C' , true);
			$this->Cell($w[1],6, number_format( $productTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[2],6, number_format( $productDiscountTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[3],6, number_format( $treatmentTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[4],6, number_format( $treatmentDiscountTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[5],6, number_format( $setTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[6],6, number_format( $setDiscountTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[7],6, number_format( $courseTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[8],6, number_format( $courseDiscountTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[9],6, number_format( $cutCourseTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[10],6, number_format( $discountTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[11],6, number_format( $toalNetTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[12],6, number_format( $payTotalTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[13],6, number_format( $smartpurse ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[14],6, number_format( $payCashTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[15],6, number_format( $payCreditTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[16],6, number_format( $paySmartTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[17],6, number_format( $creditTotal ,2,'.',','),'LR',0,'R',true);
			$this->Cell($w[18],6, number_format( $creditSmartTotal ,2,'.',','),'LR',0,'R',true);
				
			$this->Ln();	
			$this->Cell(array_sum($w),0,'','T');
			$this->Ln();
		 
	$payCashTotal=0;
			$k++;
		}
		
	}
}

if (!(isset($_POST['yFrom']))) {
	 	 header("location:report_monthly_sale.php");
}

 
	$yfrom = $_POST['yFrom'];
	$yto = $_POST['yTo'];
	
	//echo $mfrom .'-'. $mto.'-'. $yfrom.'-'. $yto;
	$branch = $_POST['branch'];

	$report=new M_report();
	 // $yfrom="2011";
	 // $yto="2011";
	 // $mfrom="3";
	 // $mto="4";
	 // $branch=0;
	
 	$header=$report->YearSaleHeader();
	$data=$report->YearSaleReport( $yfrom , $yto , $branch);
	
 if (count($data)==0){
	echo "<script>alert('Data not found');window.close();</script>";
	exit();
}
	$pdf=new PDF('L');
	$pdf->setHeaderCriteria(  $yfrom  , $yto);
	$pdf->SetRightMargin(5);
	$pdf->SetLeftMargin(5);
	//Set pageno
	$pdf->AliasNbPages();
	$pdf->AddFont("tahoma","","tahoma.php");
	$pdf->AddFont("tahoma","B","tahomab.php");

	$pdf->SetFont("tahoma","B",10); 
	$pdf->SetFont("tahoma","",10); 
 

	//Data loading
	$pdf->AddPage();
	 $pdf->GenTable($header,$data );
	 $pdf->Output();
	exit();
 
?>
